<template>
	<view class="success">
		<view class="box1">
			<u--image src="https://cdn.uviewui.com/uview/album/1.jpg" shape="circle" width="200rpx"
				height="200rpx"></u--image>
			<text>订单提交成功</text>
			<view class="item1">
				<text>应付金额</text>
				<text>￥{{total}}</text>
			</view>
		</view>
		<view class="pay">
			<u-button type="error" text="立即购买" shape @click="pay()"></u-button>
		</view>


		});

	</view>
</template>

<script>
	const db = uniCloud.database();
	const dbCmd = db.command;
	const CloudUser = uniCloud.importObject("users")
	const cloudShoppingCarts = uniCloud.importObject("shoppingCarts");
	const CloudOrder = uniCloud.importObject("order");
	const CloudRecord=uniCloud.importObject("buyRecord");
	const CloudSnack=uniCloud.importObject("snacks");
	export default {
		data() {
			return {
				total: 0,
				show: false,
				user_id: "",
				orderidList: []
			};
		},
		onLoad(e) {
			console.log(e);
			this.user_id = uni.getStorageSync("uni-id-pages-userInfo")._id;
			console.log(this.user_id);
			this.orderidList = JSON.parse(e.orderidList);
			console.log(this.orderidList);
			this.total = e.total;
		},
		methods: {
			pay() {
				uni.showModal({
						title: '提示',
						content: '确认支付?',
						success: res => {
							if (res.confirm) {
								CloudUser.getuserInfo(this.user_id).then(res => {
										console.log(res);
										if (res.data[0].surplus >= this.total) {
											CloudUser.reduceSurplus(this.user_id, this.total);
											console.log(2);
											for (let item of this.orderidList) {
													console.log(1);
													CloudSnack.updateSales(item.snack_id[0]._id)
													CloudOrder.updateOrder(item.id, 1)
													CloudRecord.addBuyRecord(this.user_id, item.snack_id[0]._id,item.id)
													cloudShoppingCarts.deleteShoppingCart(this.user_id, item.snack_id[0]._id)
			
												}
												uni.showModal({
													title: '支付成功'
												})
						
										} else {
											uni.showModal({
												title: '余额不足，支付失败'
											})
										}
									})
									 }else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
					}
				}
			}
</script>

<style lang="scss">
	.success {
		width: var(--status-bar-width);
		text-align: center;

		.box1 {
			width: 100%;
			height: 400rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			align-items: center;

			.item1 {
				display: flex;
				justify-content: space-between;
			}
		}

		.pay {
			width: 100%;
			position: fixed;
			bottom: var(--window-bottom, 20rpx);
		}
	}
</style>